package com.mo.common.lucene;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mo.common.lucene.interfaceAction.LuceneSave;
import com.mo.common.lucene.interfaceAction.LuceneUpdate;
import com.mo.common.lucene.utils.LuceneClient;

/**
 * created by harry on 2016/5/18.
 */
public class LuceneWriterManager {
    private Logger logger = LoggerFactory.getLogger(LuceneWriterManager.class);
    private static LuceneWriterManager luceneWriterManager=null;  
    private static Map<String ,LuceneWriterManager> luceneWriterManagerMap = new HashMap<String ,LuceneWriterManager>();

    //静态工厂方法   
    public static LuceneWriterManager getInstance(String indexerName) {  
    	if(indexerName==null){
    		return null;
    	}
    	if(luceneWriterManagerMap.containsKey(indexerName)){
    		luceneWriterManager = luceneWriterManagerMap.get(indexerName);
    		if(luceneWriterManager==null){
    			luceneWriterManagerMap.put(indexerName, new LuceneWriterManager(new LuceneClient(indexerName)));
    		}
    	}else{
    		luceneWriterManagerMap.put(indexerName, new LuceneWriterManager(new LuceneClient(indexerName)));
    	}
    	return luceneWriterManagerMap.get(indexerName);  
    }
    private LuceneClient luceneClient = null;
	public LuceneWriterManager(LuceneClient luceneClient){
		this.luceneClient = luceneClient;
	}

	public LuceneWriterManager(){
	}
	/**
     * 删除文档
     *
     * @throws IOException
     */
	public long delete(TermQuery delQuery) throws IOException {
		IndexWriter indexWriter = this.luceneClient.getIndexWriter();
		long count = indexWriter.deleteDocuments(delQuery);
        indexWriter.commit();
        indexWriter.close();
        System.out.println("删除完成:" + count);
        return count;
    }
	public long updateByMajorKey(LuceneUpdate update) {
		return updateByMajorKey(update,"id");
	}
	 /**
     * 测试更新， 需要注意的是：更新的条件只能是字符串
     * 实际上就是删除后新增一条
     *
     * @throws IOException
     */
	public long updateByMajorKey(LuceneUpdate update,String majorKey) {
		long start = System.currentTimeMillis();
		try {
			IndexWriter indexWriter = this.luceneClient.getIndexWriter();
			Document doc = update.updateByMajorKey();
			long count = indexWriter.updateDocument(new Term(majorKey, doc.get(majorKey)+""), doc);
			//关闭流
			indexWriter.close();
			System.out.println("修改索引花费了" + (System.currentTimeMillis() - start) + " 毫秒");
			return count;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
        //创建Document对象，存储索引
    }
	/***
	 * 创建索引 插入数据
	 * @param writer
	 * @throws IOException
	 */
	public void write(LuceneSave writer) throws IOException {
//		writer.write(luceneClient);
		try {
			long start = System.currentTimeMillis();
			IndexWriter indexWriter = this.luceneClient.getIndexWriter();
			Document doc = writer.write();
			indexWriter.addDocument(doc);
			//将doc对象保存到索引库中
			indexWriter.commit();
			//关闭流
			indexWriter.close();
			System.out.println("索引花费了" + (System.currentTimeMillis() - start) + " 毫秒");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //创建Document对象，存储索引
    }
	
}
